//
//  FindViewController.m
//  7.23 半糖
//
//  Created by dllo on 16/7/23.
//  Copyright © 2016年 高雅馨. All rights reserved.
//

#import "FindViewController.h"
#import "classifyCollectionViewCell.h"
#import "HotCollectionViewCell.h"
#import "HotCollectionReusableView.h"
#import "ClassifyCollectionReusableView.h"
#define kwidth [UIScreen mainScreen].bounds.size.width
#define KHeight [UIScreen mainScreen].bounds.size.height
#define COLORAUTO [UIColor colorWithRed:(arc4random() % 256 / 255.0 )green:(arc4random() % 256 / 255.0 ) blue:(arc4random() % 256 / 255.0 ) alpha:1.0];
@interface FindViewController ()<UIScrollViewDelegate, UICollectionViewDataSource,UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)UICollectionView * collectionView;
@property (nonatomic,strong) UIScrollView  *scrollview;
@property (nonatomic, strong) UISegmentedControl  *segement;

@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(clickRightBtn)];
    _segement = [[UISegmentedControl alloc] initWithItems:@[@"文章", @"晒单"]];
    _segement.frame = CGRectMake(90, 20, kwidth - 180, 30);
    _segement.selectedSegmentIndex = 1;
    _segement.tintColor = [UIColor colorWithRed:0.94 green:0.41 blue:0.38 alpha:1.00];
    [self.segement  addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = _segement;
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:23],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.94 green:0.41 blue:0.38 alpha:1.00];
    self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kwidth, KHeight)];
    _scrollview.directionalLockEnabled = YES;
    _scrollview.contentSize=CGSizeMake(kwidth * 2, KHeight);
    _scrollview.pagingEnabled = YES;
    _scrollview.delegate=self;
    // [self.view addSubview:_scrollview];
    [self creatHotCollection];
}
-(void)click:(UISegmentedControl *)sender{
    if (self.segement.selectedSegmentIndex == 0) {
        [self.scrollview setContentOffset:CGPointMake(0 , 0) animated:YES];
    }
    else{
        [self.scrollview setContentOffset:CGPointMake(kwidth, 0) animated:YES];
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger i = self.scrollview.contentOffset.x / kwidth;
    if (i == 0) {
        self.segement.selectedSegmentIndex = 0;
    }else{
        self.segement.selectedSegmentIndex = 1;
    }
}
// 热门推荐
-(void)creatHotCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 10) / 3, (CGRectGetWidth(self.view.bounds) - 10) / 3);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumLineSpacing = 3;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(3, 3, 3, 3);
    layout.headerReferenceSize = CGSizeMake(0, 50);
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.alwaysBounceVertical = NO;
    self.collectionView.alwaysBounceHorizontal = NO;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [self.collectionView registerClass:[HotCollectionViewCell class] forCellWithReuseIdentifier:@"hotCell"];
    [self.collectionView registerClass:[HotCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"hotHeaderCell"];
    /**
     *  1. 通过上边距实现视图添加(collectionView本质scrollView)
      self.collectionView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -295, kwidth, 290)];
     view.backgroundColor = [UIColor grayColor];
     [self.collectionView addSubview:view];
     *  2. 自定义头视图
    */
    [self.collectionView registerClass:[ClassifyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"classifyCell"];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 400);
    } else {
        return CGSizeMake(0, 50);
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 15;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
         HotCollectionViewCell *StrategyCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotCell" forIndexPath:indexPath];
        StrategyCell.myImageView.image = [UIImage imageNamed:@"default_icon_placehodler"];
        return StrategyCell;
  }
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ClassifyCollectionReusableView *classifyCell = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"classifyCell" forIndexPath:indexPath];
        return classifyCell;
    } else {
        HotCollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"hotHeaderCell" forIndexPath:indexPath];
        reuseView.backgroundColor = [UIColor whiteColor];
        return reuseView;
    }
}
- (void)clickRightBtn:(UIBarButtonItem *)sender {
    
}
@end
